package com.westos.userauth.service.impl;

import com.westos.userauth.dao.PartyDao;
import com.westos.userauth.dao.PartyRelationshipDao;
import com.westos.userauth.dao.PartyRoleDao;
import com.westos.userauth.dao.RoleTypeDao;
import com.westos.userauth.domain.PartyRelationship;
import com.westos.userauth.domain.PartyRole;
import com.westos.userauth.domain.RoleType;
import com.westos.userauth.dto.PartyRelationshipDTO;
import com.westos.userauth.dto.RoleDto;
import com.westos.userauth.service.RoleService;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.xml.crypto.Data;

import java.util.Date;
import java.util.logging.Logger;
import java.util.regex.Pattern;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private PartyRelationshipDao partyRelationshipDao;
    @Autowired
    private PartyRoleDao partyRoleDao;
    @Autowired
    private RoleTypeDao roleTypeDao;
  /*  @Autowired
    private PartyDao partyDao;*/
    private org.slf4j.Logger log = LoggerFactory.getLogger(RoleServiceImpl.class);


    // 创建roletype
    //刘健
    @Override
    public boolean createRoleType(RoleType roletype) {
        RoleType roleType1 = new RoleType();
        //return roleTypeDao.createRoleType(roletype);
        boolean flag = false;
        try {

            if (roletype == null) {
                log.error("参数为空");
                return false;
            }
            if (roletype.getRoleTypeId().equals("") || roletype.getRoleTypeId() == null) {
                log.error("参数id有问题");
                return false;
            }

            if (roletype.getRoleTypeId().length() > 20 || roletype.getRoleTypeId().length() == 0) {
                log.error("参数的角色类型id长度有问题");
                return false;
            }
            if (roletype.getParentTypeId().length() > 20 || roletype.getParentTypeId().length() == 0) {
                log.error("参数roletype的父级类型id长度不符合 ！");
                return false;
            }
            if (roletype.getDescription().length() > 225 || roletype.getDescription().length() == 0) {
                log.error("参数roletype的描述类型长度不符合要求 请重新输入");
                return false;
            }
            roleType1 = roleTypeDao.queryRoleTypeById(roletype.getRoleTypeId());
            if (roleType1 == null) {
                Date date = new Date();
                roletype.setCreatedStamp(date);
                int flag1 = roleTypeDao.insertRoleType(roletype);
                if (flag1 < 0) {
                    log.error("参数roletype为空，请重新查看！");
                    return false;
                } else {
                    log.trace("创建成功");
                    return true;
                }
            } else {
                log.info("创建失败! roletypeid已经存在数据库中");
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("创建失败");
            return false;
        }

    }


    // 创建party的角色
    //刘健
    @Override
    public boolean createPartyRole(PartyRole partyRole) {
        //return partyRoleDao.createPartyRole(partyRole);

        boolean flag = false;
        try {
            if (partyRole == null)
                return false;

            String id1 = partyRole.getRoleTypeId();
            if ("".equals(id1) || id1 == null) {
                log.error("参数partyRole的id为空请重新输入");
                return false;
            }

            if (id1.length() > 20 || id1.length() == 0) {
                log.error("参数partyRole的id长度有问题 请重新输入");
                return false;
            }

            if (partyRole.getPartyId().equals("") || partyRole.getPartyId() == null) {
                log.error("参数partyrole的id为空 不符合要求");
                return false;
            }

            if (partyRole.getPartyId().length() > 20 || partyRole.getPartyId().length() == 0) {
                log.error("参数partyrole的id长度有问题 ");
                return false;
            }
            PartyRole partyRole1 = new PartyRole();
            PartyRole partyRole2 = new PartyRole();
            partyRole1 = partyRoleDao.queryPartyRoleById(partyRole.getRoleTypeId());
            partyRole2 = partyRoleDao.queryPartyRoleByPartyId(partyRole.getPartyId());
            if (partyRole1 == null && partyRole2 == null) {

                Date date = new Date();
                partyRole.setCreatedStamp(date);
                int i = partyRoleDao.insertPartyRole(partyRole);
                if (i > 0) {
                    log.trace("创建成功");
                    return true;
                } else {
                    log.error("插入数据失败！！");
                    return false;
                }
            } else {
                log.error("数据库中存在 请检查");
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("插入数据异常");
            return false;
        }

    }

    // 删除party的角色
    //刘健
    @Override
    public boolean deletePartyRole(String roleTypeId) {
        if (roleTypeId == null) {

            log.info("参数为空 无法插入 请重试");
            return false;
        }
        if ("".equals(roleTypeId)) {
            log.info("参数id为空 请重试！");
            return false;
        }
        if (roleTypeId.length() > 20) {
            log.info("输入的id长度不符合规定");
            return false;
        }
        if ("".equals(roleTypeId)) {
            log.error("参数id有问题请重新输入");
            return false;
        }
        try {

            int n = partyRoleDao.deletePartyRole(roleTypeId);

            if (n > 0) {
                log.trace("删除成功");
                return true;
            } else {
                log.error("删除失败");
                return false;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error("有异常");
            return false;
        }
    }


    //刘健
    @Override
    public boolean createPartyRelationship(PartyRelationship partyRelationship, String relationshipName) {
        //return partyRelationshipDao.createPartyRelationship(partyRelationship,relationshipName);
        try {
            if (partyRelationship == null) {
                //logger.info("参数为空 请检查！");
                return false;
            }
            if (partyRelationship.getPartyIdFrom().equals("") || partyRelationship.getPartyIdFrom() == null) {
                //logger.severe("参数partyRelationship的partyid为空 请重新检查");
                return false;
            }
            if (partyRelationship.getPartyIdFrom().length() > 20 || partyRelationship.getPartyIdFrom().length() == 0) {
                //logger.severe("参数partyRelationship的长度不符合要求 请重新检查");
                return false;
            }
            if (partyRelationship.getPartyIdTo().equals("") || partyRelationship.getPartyIdTo() == null) {
                //logger.info("partytoid有问题  请重试");
                return false;
            }

            if (partyRelationship.getPartyIdTo().length() > 20 || partyRelationship.getPartyIdTo().length() == 0) {
                //logger.info("参数的partytoid长度不符合规定");
                return false;
            }

            if (partyRelationship.getRoleTypeIdFrom().equals("") || partyRelationship.getRoleTypeIdFrom() == null) {
                //logger.info("参数的roleTyoefromid为空 请重试");
                return false;
            }
            if (partyRelationship.getRoleTypeIdFrom().length() > 20 || partyRelationship.getRoleTypeIdFrom().length() == 0) {
                //logger.info("参数的roleTyoefromid  长度不符合要求");
                return false;
            }
            if (partyRelationship.getRoleTypeIdTo().equals("") || partyRelationship.getRoleTypeIdTo() == null) {
               // logger.info("参数的roleTypeidTo 为空 请重试");
                return false;
            }
            if (partyRelationship.getRoleTypeIdTo().length() > 20 || partyRelationship.getRoleTypeIdTo().length() == 0) {
                //logger.info("参数的roleTypeidTo 长度有问题 ");
                return false;
            }
            PartyRelationship partyRelationship1 = partyRelationshipDao.queryById(partyRelationship.getRoleTypeIdFrom());
            PartyRelationship partyRelationship2 = partyRelationshipDao.queryById(partyRelationship.getRoleTypeIdTo());
            PartyRelationship partyRelationship3 = partyRelationshipDao.queryById(partyRelationship.getPartyIdFrom());
            PartyRelationship partyRelationship4 = partyRelationshipDao.queryById(partyRelationship.getPartyIdTo());
            if (partyRelationship1 == null && partyRelationship2 == null && partyRelationship3 == null && partyRelationship4 == null) {
                Date date = new Date();
                partyRelationship.setCreatedStamp(date);
                int flag = partyRelationshipDao.insertPartyRelationship(partyRelationship, partyRelationship.getRelationshipName());
                if (flag < 0) {
                    log.error("创建失败 ");
                    return false;
                } else {
                    log.trace("创建成功");
                    return true;
                }
            } else {
                //logger.info("数据库中已经存在 有该主键的数据");
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("创建失败");
            return false;
        }

    }

    /**
     * author：史思潮
     *
     * @param partyRelationship
     * @return
     */
    @Override
    public boolean updatePartyRelationship(PartyRelationship partyRelationship) {
        /*** 校验thrudate是否为空
         */
        //调用Dao层接口查询
        try {

            if (partyRelationship == null) {
                //logger.severe("日期为null,无法更新");
                return false;
            }
            partyRelationship.setThruDate(new Date());
            int row = partyRelationshipDao.updatePartyRelationship(partyRelationship);
            if (row > 0){
                //logger.fine("更新成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            //logger.severe("更新失败!");
        }
        return true;
    }

    /**
     * author：卢梓浩
     *
     * @param partyRelationshipDTO
     * @return
     */
    // 删除party的关系
    @Override
    public boolean deletePartyRelationship(PartyRelationshipDTO partyRelationshipDTO) {
        try {

            if (partyRelationshipDTO == null) {
                return false;
            }
            if (null == partyRelationshipDTO.getPartyIdFrom()
                    || null == partyRelationshipDTO.getPartyIdTo()
                    || null == partyRelationshipDTO.getRoleTypeIdFrom()
                    || null == partyRelationshipDTO.getRoleTypeIdTo())
                return false;
            if ("".equals(partyRelationshipDTO.getPartyIdFrom())
                    || "".equals(partyRelationshipDTO.getPartyIdFrom())
                    || "".equals(partyRelationshipDTO.getRoleTypeIdFrom())
                    || "".equals(partyRelationshipDTO.getRoleTypeIdTo())) {
                log.error("输入有误！！！");
                return false;
            }

            //初始时间不存在的话，不需要删除，返回true
            if (partyRelationshipDTO.getFromDate() == null) {
                return true;
            }
            //代码如果可以执行到这，代表有初始时间，设置为当前时间为空为删除
            partyRelationshipDTO.setFromDate(null);
            int i = partyRelationshipDao.deletePartyRelationship(partyRelationshipDTO.getFromDate());
            if (i > 0)
                return true;
            else
                return false;

        } catch (Exception e) {
            log.error("执行dao层代码出错！");
            e.printStackTrace();
            return false;
        }
    }

    /**
     * author：卢梓浩
     *
     * @param roleDto
     * @return
     */
    // 创建party的关系和角色
    @Override
    public boolean createPartyRelationshipAndRole(RoleDto roleDto) {

        try {
            if (null == roleDto.getRoleTypeId() || null == roleDto.getRelationshipName()) {
                return false;
            }
            if ("".equals(roleDto.getRoleTypeId()) || "".equals(roleDto.getRelationshipName())) {
                log.error("id或关系名为空");
                return false;
            }

            //设置当前时间
            roleDto.setFromDate(new Date());
            int i = partyRelationshipDao.insertPartyRelationshipAndRole(
                    roleDto.getRoleTypeId(),
                    roleDto.getRelationshipName(),
                    roleDto.getFromDate());
            if (i > 0)
                return true;
            else
                return false;
        } catch (Exception e) {
            log.error("执行dao层代码出错！");
            e.printStackTrace();
            return false;
        }

    }

    /**
     * author：卢梓浩
     *
     * @param partyRelationshipTypeId
     * @return
     */

    //创建关系类型
    @Override
    public int createPartyRelationshipType(String partyRelationshipTypeId) {

        try {
            if (null == partyRelationshipTypeId)
                return -1;
            if ("".equals(partyRelationshipTypeId)) {
                log.error("partyRelationshipTypeId为空！");
                return -1;
            }

            int i = partyRelationshipDao.insertPartyRelationshipType(partyRelationshipTypeId);
            if (i > 0)
                return 1;
            else
                return -1;
        } catch (Exception e) {
            log.error("执行dao层代码出错！");
            e.printStackTrace();
            return -1;
        }

    }

    /**
     * author：卢梓浩
     *
     * @param roleDto
     * @return
     */
    // 创建更新party的关系和角色
    @Override
    public int createUpdatePartyRelationshipAndRoles(RoleDto roleDto) {
        try {
            if (null == roleDto.getRoleTypeId() || null == roleDto.getRelationshipName()) {
                return -1;
            }
            if ("".equals(roleDto.getRoleTypeId()) || "".equals(roleDto.getRelationshipName())) {
                log.error("id或关系名为空");
                return -1;
            }

            //如果用户不存在，就创建
            Date fromDate = roleDto.getFromDate();
            if (fromDate == null) {
                roleDto.setFromDate(new Date());
                int i = partyRelationshipDao.insertUpdatePartyRelationshipAndRoles(
                        roleDto.getRoleTypeId(),
                        roleDto.getRelationshipName(),
                        roleDto.getFromDate());
                return i;
            }

            //执行到这里就是用户存在，需要更新时间
            roleDto.setThruDate(new Date());
            int i = partyRelationshipDao.UpdatePartyRelationshipAndRoles(roleDto.getThruDate());
            return i;


        } catch (Exception e) {
            log.error("执行dao层代码出错！");
            e.printStackTrace();
            return -1;
        }

    }


    public PartyRelationshipDao getPartyRelationshipDao() {
        return partyRelationshipDao;
    }

    public void setPartyRelationshipDao(PartyRelationshipDao partyRelationshipDao) {
        this.partyRelationshipDao = partyRelationshipDao;
    }

    public PartyRoleDao getPartyRoleDao() {
        return partyRoleDao;
    }

    public void setPartyRoleDao(PartyRoleDao partyRoleDao) {
        this.partyRoleDao = partyRoleDao;
    }

    public RoleTypeDao getRoleTypeDao() {
        return roleTypeDao;
    }

    public void setRoleTypeDao(RoleTypeDao roleTypeDao) {
        this.roleTypeDao = roleTypeDao;
    }
}
